/**
 * Copyright (c) 2012 Ben Murphy (Smurph)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.smurph.expandablelist;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.smurph.expandablelist.database.DBHelper;
import com.smurph.expandablelist.database.MyExpandableListAdapter;
import com.smurph.expandablelist.database.QueryHandler;
import com.smurph.expandablelist.provider.TblABC.ABCs;
import com.smurph.expandablelist.provider.TblName.Names;

public class StartingActivity extends ExpandableListActivity implements
		OnChildClickListener {

	private static QueryHandler mQueryHandler;
	private CursorTreeAdapter mAdapter;

	public static void runChildQuery(int position, long abc_ID) {

		Uri.Builder builder = Names.CONTENT_URI_BY_ABC_ID.buildUpon();
		ContentUris.appendId(builder, abc_ID);
		Uri lNames = builder.build();

		mQueryHandler.startQuery(QueryHandler.TOKEN_CHILD, position, lNames,
				null, null, null, Names.TBL_NAMES + "." + Names.SORT_ORDER_ASC);
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {

		TextView txt = (TextView) v.findViewById(android.R.id.text1);

		Toast.makeText(
				this,
				"You clicked " + txt.getText().toString() + " in group "
						+ groupPosition + ".", Toast.LENGTH_SHORT).show();

		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Set up our adapter
		mAdapter = new MyExpandableListAdapter(this,
				android.R.layout.simple_expandable_list_item_1,
				android.R.layout.simple_expandable_list_item_1,
				new String[] { ABCs.LETTER }, // Text for group layouts
				new int[] { android.R.id.text1 },
				new String[] { Names.NAME }, // Text for child layouts
				new int[] { android.R.id.text1 });

		setListAdapter(mAdapter);

		mQueryHandler = new QueryHandler(this.getContentResolver(), mAdapter);

		// Query for groups
		mQueryHandler.startQuery(QueryHandler.TOKEN_GROUP, null,
				ABCs.CONTENT_URI, ABCs.DEFAULT_PROJECTION, ABCs.ACTIVE + "=?",
				new String[] { "1" }, ABCs.SORT_ORDER_ASC);

		getExpandableListView().setOnChildClickListener(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mAdapter.changeCursor(null);
		mAdapter = null;

		File db = new File(Environment.getExternalStorageDirectory()
				+ "/smurph/" + DBHelper.mDBName);

		if (!db.getParentFile().exists())
			db.getParentFile().mkdirs();

		if (db.exists())
			db.delete();

		try {
			RandomAccessFile to = new RandomAccessFile(db, "rw");
			RandomAccessFile from = new RandomAccessFile(
					"/data/data/com.smurph.expandablelist/databases/"
							+ DBHelper.mDBName, "r");

			FileChannel chTo = to.getChannel();
			FileChannel chFrom = from.getChannel();

			chFrom.transferTo(0, from.length(), chTo);

			chTo.close();
			chFrom.close();
			to.close();
			from.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}